home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / UUCONVER.ZIP / uuconver.c
C/C++ Source or Header  |  1994-12-12  |  7KB  |  226 lines

  1. /*
  2.      By popular demand, here is the C source code for a program called
  3. UUConvert.  It's the best.  I think this source may be compiled on
  4. every type of computer (I've successfully compiled it without changes on
  5. several types of UNIX machines, a VMS machine and even my Amiga).
  6.  
  7.      What can this do?  Well, it will simply uudecode uuencoded files.  The
  8. nice thing about is that it will accept a file that has been cut into pieces
  9. (handy for those VMS news readers) or accept several seperate uuencoded files
  10. in one big bundled file.  It will even clip away all excess garbage from the
  11. header and even in between!  No need to spend hours editing uuencoded text
  12. files!
  13.  
  14.      The instructions are included at the end of the program (commented of
  15. course).  Simply cut of everything the dashed line (including the line) and
  16. run it through your favorite C compiler.  Please don't ask me for help
  17. compiling....It's too simple to screw up.  If you really need help, consult
  18. the manual which comes with the C compiler.
  19.  
  20. */
  21.  
  22. #include <stdio.h>
  23. #include <string.h>
  24.  
  25. #define TRUE    1
  26. #define FALSE   0
  27. #define DEC(c)  (((c) - ' ') & 077)
  28. #define LENGTH  150
  29.  
  30. extern void uuread();
  31. FILE *out;
  32.  
  33. void main(argc, argv)
  34. int argc;
  35. char *argv[];
  36. {
  37.   int error=FALSE, argno;
  38.   FILE *infile;
  39.  
  40.   if (argc < 2)
  41.     uuread(stdin);
  42.   else
  43.     for (argno = 1; !error && argno < argc; argno++)
  44.       if (error = ((infile = fopen(argv[argno], "r")) == NULL))
  45.         fprintf(stderr, "uucat: Can't open '%s' for input.\n", argv[argno]);
  46.       else
  47.         uuread(infile), fclose(infile);
  48.   exit(0);
  49. }
  50.  
  51.  
  52. void uuread(infile)
  53. FILE *infile;
  54. {
  55.   char *s2, *s1, *s0, *tmp_s;
  56.   int length;
  57.   static char s[3 * (LENGTH + 1)];
  58.   static int echo_on = FALSE, started = FALSE, just_finished = FALSE;
  59.   static int line_length = 0, lines_to_go = 0, firstline=FALSE;
  60.  
  61.   s0 = s;
  62.   s1 = s0 + (LENGTH + 1);
  63.   s2 = s1 + (LENGTH + 1);
  64.  
  65.   s0[0] = s1[0] = s2[0] = '\0';  /* Clear strings */
  66.  
  67.   while (fgets(s0, LENGTH, infile) != NULL)
  68.   {
  69.     s0[LENGTH] = '\0';  /* Make sure string is terminated */
  70.  
  71.     if (just_finished)
  72.       just_finished = FALSE; /* yea I don't why either */
  73.  
  74.     if (!started)
  75.       {
  76.         if (strncmp(s0, "begin ", 6) == 0)
  77.           {
  78.             int mode;
  79.             char dest[128];
  80.             firstline=TRUE;
  81.             started = echo_on = TRUE, line_length = 0, lines_to_go = 0;
  82.             sscanf(s0, "begin %o %s", &mode, dest);
  83.             out = fopen(dest, "w");
  84.             if (out == NULL) perror(dest), exit(4);
  85.           }
  86.       }
  87.     else  /* started */
  88.     {
  89.       length = strlen(s0);
  90.       if (line_length == 0)
  91.         line_length = length;
  92.  
  93.       if (echo_on)
  94.       {
  95.         lines_to_go = 0;
  96.         if (s0[0] != 'M' || length != line_length)
  97.         {
  98.           echo_on = FALSE;
  99.           lines_to_go = 2;  /* Lines to go before 'end' is expected */
  100.           if (s0[0] == ' ' || s0[0] == '`')
  101.             lines_to_go = 1;
  102.         }
  103.       }
  104.       else  /* !echo_on */
  105.         if (s0[0] == 'M' && length == line_length)
  106.           echo_on = TRUE;
  107.         else if (lines_to_go > 0)
  108.           if (lines_to_go == 2)
  109.             if (s0[0] == ' ' || s0[0] == '`')
  110.               lines_to_go = 1;
  111.             else
  112.               lines_to_go = 0;  /* Unexpected line, so break off */
  113.           else if (lines_to_go == 1)
  114.             if (strcmp(s0, "end\n") == 0)
  115.             {
  116.               doaline(s2, stdout);
  117.               doaline(s1, stdout);
  118.               lines_to_go = 0;  /* Done. Break off */
  119.               just_finished = TRUE;
  120.               started = FALSE;
  121.             }
  122.             else
  123.               lines_to_go = 0;  /* Unexpected line, so break off */
  124.     }
  125.  
  126.     if (echo_on&&(!firstline))
  127.       doaline(s0, stdout), s0[0] = '\0';
  128.     else if (firstline)
  129.       firstline=FALSE;
  130.  
  131.     tmp_s = s2, s2 = s1, s1 = s0, s0 = tmp_s;
  132.   }
  133. }
  134.  
  135. /*
  136.  * copy from in to out, decoding as you go along.
  137.  */
  138. doaline(s)
  139.      char s[];
  140. {
  141.   char *bp;
  142.   int n;
  143.   if ((n=DEC(s[0])) > 0)
  144.     {
  145.       bp = &s[1];
  146.       while (n > 0)
  147.         outdec(bp, n), bp += 4, n -= 3;
  148.     }
  149. }
  150.  
  151. /*
  152.  * output a group of 3 bytes (4 input characters).
  153.  * the input chars are pointed to by p, they are to
  154.  * be output to file f.  n is used to tell us not to
  155.  * output all of them at the end of the file.
  156.  */
  157. outdec(p, n)
  158.      char *p;
  159. {
  160.   int c1, c2, c3;
  161.  
  162.   c1 = DEC(*p) << 2 | DEC(p[1]) >> 4;
  163.   c2 = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
  164.   c3 = DEC(p[2]) << 6 | DEC(p[3]);
  165.   if (n >= 1) putc(c1, out);
  166.   if (n >= 2) putc(c2, out);
  167.   if (n >= 3) putc(c3, out);
  168. }
  169.  
  170. /*
  171.  
  172. INSTRUCTIONS
  173. ---------------
  174.  
  175. uuconvert.man:
  176. ***********
  177. Hey,  A while back ago somebody posted the source code for a program named
  178. uucat which had the ability to strip headers from the uuencoded files that
  179. are posted to various newsgroups.  Then a couple of months later another
  180. person posted the source codes for uudecode and uuencode.  Then I said hey!
  181. I`m a computer Scientist, why do I have to use two utilies to get the job
  182. done?  So I combined the two programs into one, cleaned up the code (or made
  183. it more cryptic, it's 'c' I'll let you decide)  Now this program will strip
  184. headers and tailers from files and uudecode the contents at the same time.
  185.  
  186. (I'm sorry about the fact that this program is compatible only with unix, as I
  187. stripped out the #ifndef's for compiling on other machines.  But I don't
  188. think it would be very hard to change the few things that cause portability
  189. problem.  try it as is, it may work for you.)
  190.  
  191. To use the program once it is compiled simply use it like-
  192.  
  193. cat file1 file2 file*... | uuconvert
  194. or
  195. uuconvert file1 file2 file* ...  for as many files as you have.
  196.  
  197. (it doesn't need to be named uuconvert, you can name it what ever you wish.)
  198. so this is extreemly handy. it will also uudecode files that do not
  199. have headers or tailers.  I DO NOT MAKE ANY PROMISES AS TO HOW WELL
  200. IT WORKS!!  I only worked on it a couple of hours.  But I have
  201. decoded several of the .gif files from this newsgroup and it worked
  202. fine for all of them, I.E. no SHORT FILEs.  If something does go wrong
  203. however it's up to you to fix it.
  204.  
  205. Now it is sooo easy to use things like -
  206.  
  207. What Next [nNq]   2578-2562 S tempfile       // from news
  208.  
  209. and then from the local machine to just say
  210.  
  211. uuconvert tempfile
  212.  
  213. and it will change the muliple article file 'tempfile' into it's
  214. corresponding .gif file or whatever type was uuencoded.  So copy
  215. this down remove this explanation, compile and enjoy.
  216. oh yea, it does not remove the 'tempfile' so you have to remember to
  217. do that yourself if you don't want it hanging around.
  218.  
  219. If you have any questions about it's use please send mail to
  220. wiegley@sun.acsu.buffalo.edu
  221. (no complaints please I didn't write it entirely,  and I give thanks to
  222. those who did the majority of code research and development, sorry guys,
  223. I forgot your names.)
  224.  
  225. */
  226.